# -*- coding: utf-8 -*-
#!/usr/bin/env python3
import numpy as np
samples_and = [
    [0, 0, 0],
    #[0, 1, 0],
    #[1, 0, 0],
    [1, 0, 0],
    [0, 1, 0],
    [1, 1, 1]
]

samples_or = [
    [0, 0, 0],
    #[0, 1, 1],
    #[1, 0, 1],
    [1, 0, 1],
    [0, 1, 1],
    [1, 1, 1]
]

samples_xor = [
    [0, 0, 0],
    #[0, 1, 1],
    #[1, 0, 1],
    [1, 0, 1],
    [0, 1, 1],
    [1, 1, 0]
]

def perceptron(samples):
    w = np.array([-1, -1])
    bias = 0
    coef = 1

    for i in range(10):
        for j in range(len(samples)):
            x = np.array(samples[j][:2])
            y = 1 if np.dot(w, x) + bias > 0 else 0
            t = np.array(samples[j][-1])
            
            delta_b = coef * (t - y)
            delta_w = coef * (t - y) * x

            print('epoch {} smaple {} -- w:{}\tbias:{}\t\ty:{}\tdelta_w:{}\t\tdelta_b:{}'.format(i, j, w, bias, y, delta_w, delta_b))
            w = w + delta_w
            bias = bias + delta_b

if __name__ == '__main__':
    if False:
        print('logical and:')
        perceptron(samples_and)
    if False:
        print('\nlogical or:')
        perceptron(samples_or)
    if True:
        print('\nlogical xor:')
        perceptron(samples_xor)